#/bin/sh
############################################################################
# Ralink Tech Inc.
# 4F, No. 2 Technology 5th Rd.
# Science-based Industrial Park
# Hsin-chu, Taiwan, R.O.C.
#
# (c) Copyright 2002-2006, Ralink Technology, Inc.
#
# All rights reserved. Ralink's source code is an unpublished work and the
# use of a copyright notice does not imply otherwise. This source code
# contains confidential trade secret material of Ralink Tech. Any attempt
# or participation in deciphering, decoding, reverse engineering or in any
# way altering the source code is stricitly prohibited, unless the prior
# written consent of Ralink Technology, Inc. is obtained.
############################################################################

#
# Change uboot/kernel size if necessary
#

#-------
# Notes: UBOOT_SIZE + KERNEL_SIZE = 8MB
#-------

UBOOT_SIZE = 0x50000
KERNEL_SIZE = 0x7B0000

#-------
# Notes: KERNEL_FIRST_PART = 0x400000 - UBOOT_SIZE
#-------
KERNEL_FIRST_PART = 0x3B0000  # 0x400000 - 0x50000 = 0x3B0000 
KERNEL_FIRST_PART_DECIMAL =`printf "%d" $(KERNEL_FIRST_PART)`

#-----------------------------------------------

USER_NAME  = $(shell whoami)

#
# Uboot Image Information
#
UBOOT_DIR = .
UBOOT_IMAGE = uboot.bin

#
# Linux Kernel Image Information
#
KERNEL_DIR = .
KERNEL_IMAGE = root_uImage


#
# Single Image Information
#
PACKED_DIR = .
PACKED_IMAGE = root_uImage.img

all: check_img

	@echo ""
	@echo "Flash layout:"
	@echo " +-----------------------+-------+------+---+--------------------+"
	@echo " |   KERNEL PartII       | Uboot |UsrCfg| RF| Kernel PartI       |"
	@echo " +-----------------------+-------+------+---+--------------------+"
	@echo " |<------0x400000------->|<----$(UBOOT_SIZE)----->|<----$(KERNEL_FIRST_PART)---->|"
	
	@echo -e "\n-Original Uboot Image Size"
	@wc -c $(UBOOT_DIR)/$(UBOOT_IMAGE)



# Uboot Padding
	@cp $(UBOOT_DIR)/$(UBOOT_IMAGE) $(UBOOT_DIR)/$(UBOOT_IMAGE)_padding
	@SIZE=`wc -c $(UBOOT_DIR)/$(UBOOT_IMAGE) | awk '{ print $$1 }'` ; \
	UBOOT_PART_SIZE=`printf "%d" $(UBOOT_SIZE)` ; \
	PAD=`expr $$UBOOT_PART_SIZE - $$SIZE` ; \
	dd if=/dev/zero count=1 bs=$$PAD 2> /dev/null | \
	tr \\000 \\377 >> $(UBOOT_DIR)/$(UBOOT_IMAGE)_padding
	
	@echo -e "\n-Padded Uboot Image Size"
	@wc -c $(UBOOT_DIR)/$(UBOOT_IMAGE)_padding

	@echo -e "\n-Original Kernel Image Size"
	@wc -c $(KERNEL_DIR)/$(KERNEL_IMAGE)

# Kernel Padding
	@cp $(KERNEL_DIR)/$(KERNEL_IMAGE) $(KERNEL_DIR)/$(KERNEL_IMAGE)_padding
	@SIZE=`wc -c $(KERNEL_DIR)/$(KERNEL_IMAGE) | awk '{ print $$1 }'` ; \
	KERNEL_PART_SIZE=`printf "%d" $(KERNEL_SIZE)` ; \
	PAD=`expr $$KERNEL_PART_SIZE - $$SIZE` ; \
	dd if=/dev/zero count=1 bs=$$PAD 2> /dev/null | \
	tr \\000 \\377 >> $(KERNEL_DIR)/$(KERNEL_IMAGE)_padding

	@echo -e "\n-Padded Kernel Image Size"
	@wc -c $(KERNEL_DIR)/$(KERNEL_IMAGE)_padding

# Split Kernel (generate two files - xaa, xab, xac)
	@echo -e "\n-Split Kernel Image Size to following parts"
	@split -b $(KERNEL_FIRST_PART_DECIMAL) $(KERNEL_DIR)/$(KERNEL_IMAGE)_padding
	@ls -l xa*

# Copy Kernel PartII
	@echo -e "\n-Generate Final Image"
	@cp xab $(PACKED_DIR)/$(PACKED_IMAGE)
	@cat xac >> $(PACKED_DIR)/$(PACKED_IMAGE)

# Copy Uboot & Kernel PartI
	@cat $(UBOOT_DIR)/$(UBOOT_IMAGE)_padding >>  $(PACKED_DIR)/$(PACKED_IMAGE)
	@cat xaa >> $(PACKED_DIR)/$(PACKED_IMAGE)
	@wc -c $(PACKED_DIR)/$(PACKED_IMAGE)
	@rm -f xa* *_padding

clean:
	rm -f $(PACKED_DIR)/$(PACKED_IMAGE) 

check_img:
	@if [ ! -f $(UBOOT_DIR)/$(UBOOT_IMAGE) ]; then \
	echo "" ; \
	echo "Please copy \"$(UBOOT_IMAGE)\" to current directory"; \
	echo "" ; \
	exit 1; \
	fi 

	@if [ ! -f $(KERNEL_DIR)/$(KERNEL_IMAGE) ]; then \
	echo "" ; \
	echo "Please copy \"$(KERNEL_IMAGE)\" to current directory"; \
	echo "" ; \
	exit 1; \
	fi
